import {
  queryOrder,
  passOrder,
  cancalOrder
} from "../../utils/config"
import store from "../../utils/factory"
let that = null;
const util = require('../../utils/util.js')

Page({
  data: {
    showtab: 0, //顶部选项卡索引
    limit: 10,
    page: 1,
    noData: false,
    finish: false,
    status: "",
    submitStatus: false,
    tabnav: {
      tabnum: 5,
      tabitem: [{
          "id": 0,
          "text": "全部"
        },
        {
          "id": 1,
          "text": "待付款"
        },
        {
          "id": 2,
          "text": "待发货"
        },
        {
          "id": 3,
          "text": "待收货"
        },
        {
          "id": 4,
          "text": "已完成"
        },
        {
          "id": 5,
          "text": "已退款"
        },
        {
          "id": 6,
          "text": "已评价"
        },
      ]
    },
    productList: [],
  },
  onLoad: function (e) {
    that = this;
    if (e) {
      this.transferStauts(e.status)
    }
  },
  transferStauts(status) {
    let a = status;
    switch (a) {
      case "waitPay":
        this.setData({
          status: "待付款",
          showtab: 1
        })
        break;
      case "waitDeliver":
        this.setData({
          status: "待发货",
          showtab: 2
        })
        break;
      case "waitDeliver":
        this.setData({
          status: "待收货",
          showtab: 3
        })
        break;
      case "finish":
        this.setData({
          status: "已完成",
          showtab: 4
        })
        break;
      case "finishRefund":
        this.setData({
          status: "已退款",
          showtab: 5
        })
        break;
      default:
        this.setData({
          status: "",
          showtab: 0
        })
    }
  },
  onShow() {
    that = this;
    this.initPageStatus();
  },
  evaluate(e) {
    store.set("orderId", e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../evaluate/evaluate'
    })
  },
  cancelOrder(e) {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    let orderDetail = e.currentTarget.dataset.obj
    wx.showModal({
      title: "温馨提示",
      content: "是否确认取消订单",
      showCancel: true,
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          wx.showLoading({
            title: "取消订单中..."
          })
          cancalOrder([orderDetail.id]).then(res => {
            wx.hideLoading();
            that.initPageStatus()
            wx.showToast({
              title: '取消订单成功',
              duration: 1000
            })

          }).catch(err => {
            wx.hideLoading()
            that.initPageStatus()
          })
        }
      }
    })
  },
  initsubmitStatus() {
    that.setData({
      submitStatus: false
    })
  },
  initPageStatus() {
    this.setData({
      finish: false,
      page: 1,
      noData: false,
      submitStatus: false,
      productList: []
    })
    this.queryOrder();
  },
  submit(e) {
    let orderDetail = e.currentTarget.dataset.obj;
    let submitOrderDetail = {
      shopCarList: orderDetail.items,
      total: orderDetail.total + store.get("express"),
      status: orderDetail.status,
      id: orderDetail.id
    }
    store.set("submitOrderDetail", submitOrderDetail)
    wx.navigateTo({
      url: '../submitOrder/submitOrder',
      success() {
        that.initsubmitStatus()
      }
    })
  },
  pass(e) {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    let orderDetail = e.currentTarget.dataset.obj;
    wx.showModal({
      title: "温馨提示",
      content: "是否确认收货",
      showCancel: true,
      success: function (res) {
        that.initsubmitStatus()
        if (res.confirm) {
          passOrder({
            id: orderDetail.id
          }).then(res => {
            that.initPageStatus();
            wx.showToast({
              title: '确认收货成功',
              duration: 1000
            })
          })
        }
      }
    })
  },
  refund(e) {
    let orderDetail = e.currentTarget.dataset.obj;
    store.set("refundOrderDetail", orderDetail)
    wx.navigateTo({
      url: '../refundOrder/refundOrder'
    })
  },
  getOrderDetail(e) {
    let edata = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../orderDetail/orderDetail?id=${edata.id}` // 页面 B
    })
  },
  setTab: function (e) {
    const edata = e.currentTarget.dataset;
    if (this.data.showtab == edata.tabindex) {
      return false
    }
    this.setData({
      showtab: edata.tabindex,
      productList: [],
      page: 1,
      noData: false,
      status: edata.status == "全部" ? "" : edata.status
    })
    this.queryOrder()
  },
  queryOrder() {
    queryOrder({
      page: this.data.page,
      limit: 10,
      status: this.data.status
    }).then(res => {
      if (res.data.length == 0) {
        this.setData({
          finish: true,
          noData: true
        })
        return false
      }
      res.data.map(item => {
        item.createTime = util.formatTime(new Date(item.createTime));
      })
      if (res.data.length < 10) {
        this.setData({
          finish: true
        })
      }
      this.setData({
        productList: [...res.data, ...this.data.productList],
      })
    })
  },
  onReachBottom() {
    console.log("bottom")
    if (this.data.finish) {
      return false
    }
    this.setData({
      page: this.data.page + 1
    })
    this.queryOrder()
  }
})